package com.hbzhit.modules.sys.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbzhit.common.utils.Constant;
import com.hbzhit.common.utils.MapUtils;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.config.CacheConfig;
import com.hbzhit.modules.sys.dao.SysMenuDao;
import com.hbzhit.modules.sys.entity.SysMenuEntity;
import com.hbzhit.modules.sys.service.SysMenuService;
import com.hbzhit.modules.sys.service.SysRoleMenuService;
import com.hbzhit.modules.sys.service.SysUserService;
import com.hbzhit.modules.sys.vo.SysMenuQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	@Override
	public List<SysMenuEntity> listAll() {
		return this.list(new LambdaQueryWrapper<SysMenuEntity>()
				.eq(SysMenuEntity::getIsDel, Constant.INT_N)
				.or()
				.eq(SysMenuEntity::getIsDel, Constant.INT_Y)
		);
	}

	@Override
	public List<SysMenuEntity> queryListParentId(Long parentId) {
		return this.list(new LambdaQueryWrapper<SysMenuEntity>()
				.eq(SysMenuEntity::getParentId, parentId)
				.orderByAsc(SysMenuEntity::getOrderNum)
		);
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(SysMenuQueryVO params) {
		Long userId = ShiroUtils.getUserId();
		//系统管理员，拥有最高权限
		if(userId == Constant.SUPER_ADMIN){
			return this.list(new LambdaQueryWrapper<SysMenuEntity>()
					.eq(params.getProp() != null, SysMenuEntity::getProp, params.getProp())
					.ne(SysMenuEntity::getType, Constant.MenuType.BUTTON.getValue())
					.orderByAsc(SysMenuEntity::getOrderNum)
			);
		}

		//用户菜单列表
		return sysUserService.queryAllMenu(userId, params.getProp());
	}

	@Override
	public boolean save(SysMenuEntity entity) {
		boolean ret = super.save(entity);

		//更新缓存
		CacheConfig.menuCache.put(entity.getId(), entity);

		return ret;
	}

	@Override
	public boolean updateById(SysMenuEntity entity) {
		boolean ret = super.updateById(entity);

		//更新缓存
		CacheConfig.menuCache.put(entity.getId(), entity);

		return ret;
	}

	@Override
	public void delete(Long menuId) {
		//删除菜单
		this.removeById(menuId);
		//删除菜单与角色关联
		sysRoleMenuService.removeByMap(new MapUtils().put("menu_id", menuId));
		//更新缓存
		CacheConfig.menuCache.get(menuId).setIsDel(Constant.INT_Y);
	}

	@Override
	public List<SysMenuEntity> queryAllChildren(Long parentId){
		List<SysMenuEntity> allList = new ArrayList<>();
		CacheConfig.menuCache.forEach(menu -> {
			if (menu.getIsDel() == Constant.INT_N) {
				allList.add(menu);
			}
		});
		return getAllChildrenList(allList, parentId);
	}

	/**
	 * 递归查询所有子节点
	 * @param allList 菜单集合
	 * @param parentId 父级id
	 * @return 子菜单集合
	 */
	private List<SysMenuEntity> getAllChildrenList(List<SysMenuEntity> allList, Long parentId) {
		List<SysMenuEntity> childrenList = new ArrayList<>();
		// 判空
		if (allList == null || allList.size() == 0) {
			return childrenList;
		}
		// 找出所有的下一级节点
		for (SysMenuEntity menu : allList) {
			if (parentId.equals(menu.getParentId())) {
				childrenList.add(menu);
			}
		}
		// 当前所有子级集合
		List<SysMenuEntity> allChildrenList = new ArrayList<>(childrenList);
		// 遍历下一级节点
		if (CollUtil.isNotEmpty(childrenList)) {
			for (SysMenuEntity menu : childrenList) {
				// 递归
				List<SysMenuEntity> children = getAllChildrenList(allList, menu.getId());
				if (CollUtil.isNotEmpty(children)) {
					allChildrenList.addAll(children);
				}
			}
		}
		return allChildrenList;
	}
}
